const app=getApp()
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function post(url,data,sFn,cFn,fFn,soFn){
  let that=this,a=data;
  console.log()
  a.token=wx.getStorageSync('userData').token
  wx.request({
    url:app.globalData.url + url,
    data:a,
    method:'POST',
    success(res){
      if(res.statusCode ==200 && res.data.code==1){
        if(typeof sFn ==='function'){sFn(res)}
      }else{
        if(typeof soFn ==='function'){soFn(res)}
        else if(typeof fFn ==='function'){fFn(res)}
        else{
          wx.showToast({
            icon:'none',
            title: res.data.msg,
          })
        }
      }
    },
    fail(res){
      if(typeof fFn ==='function'){fFn(res)}
      else{
        wx.showToast({
          icon:'none',
          title: '网络错误',
        })
      }
    },
    complete(res){
      if(typeof cFn ==='function'){cFn(res)}
    }
  })
}

module.exports = {
  formatTime: formatTime,
  post:post
}
